<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use app\common\model\Opinion as OpinionModel;

class Opinion extends AdminBase
{
    protected $model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new OpinionModel();
    }

    public function index()
    {
        $map = [];
        $keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : '';
        $status = isset($_REQUEST['status']) ? trim($_REQUEST['status']) : '';

        if ($keyword) {
            $map['content'] = ['like', "%{$keyword}%"];
        }
        if ($status !== '') {
            $map['status'] = $status;
        }

        $list = $this->model->where($map)->order('id desc')->paginate(10);
        $this->assign(array('keyword' => $keyword,'status'=>$status,'list'=>$list));
        return $this->fetch('index');
    }


    public function alldelete()
    {
        $params = input('post.');
        $ids = implode(',', $params['ids']);
        $result = $this->model->destroy($ids);
        if ($result) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }

    public function delete($id)
    {
        if ($this->model->destroy($id)) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }

    public function toggle($id, $status, $name)
    {
        if ($this->request->isGet()) {

            if ($this->model->where('id', $id)->update([$name => $status]) !== false) {
                return json(array('code' => 200, 'msg' => '更新成功'));
            } else {
                return json(array('code' => 0, 'msg' => '更新失败'));
            }
        }

    }
}